package com.nexgo.payment.trans.setting.transmanage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 传统交易控制
 */
public class SetupTransSwitchActivity extends SaveParameterActivity {

    @BindView(R.id.trans_sale)
    SwitchButton transSale;
    @BindView(R.id.trans_void)
    SwitchButton transVoid;
    @BindView(R.id.trans_refund)
    SwitchButton transRefund;
    @BindView(R.id.trans_query)
    SwitchButton transQuery;
    @BindView(R.id.trans_pre_auth)
    SwitchButton transPreAuth;
    @BindView(R.id.trans_auth_void)
    SwitchButton transAuthVoid;
    @BindView(R.id.trans_complete)
    SwitchButton transComplete;
    @BindView(R.id.trans_complete_void)
    SwitchButton transCompleteVoid;
    @BindView(R.id.trans_complete_notify)
    SwitchButton transCompleteNotify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_trans_switch);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.trans_switch_control))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.SALE_SWITCH, transSale.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.SALE_VOID_SWITCH, transVoid.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.REFUND_SWITCH, transRefund.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.QUERY_SWITCH, transQuery.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_SWITCH, transPreAuth.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_VOID_SWITCH, transAuthVoid.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_COMPLETE_SWITCH, transComplete.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_COMPLETE_VOID_SWITCH, transCompleteVoid.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_COMPLETE_NOTIFY_SWITCH, transCompleteNotify.isChecked() ? "1" : "0");
        return map;
    }

    @Override
    public void showDetail() {
        transSale.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.SALE_SWITCH)));
        transVoid.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.SALE_VOID_SWITCH)));
        transRefund.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.REFUND_SWITCH)));
        transQuery.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QUERY_SWITCH)));
        transPreAuth.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_SWITCH)));
        transAuthVoid.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_VOID_SWITCH)));
        transComplete.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_COMPLETE_SWITCH)));
        transCompleteVoid.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_COMPLETE_VOID_SWITCH)));
        transCompleteNotify.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_COMPLETE_NOTIFY_SWITCH)));
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()){
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupTransSwitchActivity.class);
    }
}
